//
//  mineViewController.m
//  WHB_driver
//
//  Created by 徐国庆 on 15/12/8.
//  Copyright (c) 2015年 turing. All rights reserved.
//

#import "mineViewController.h"
#import "AccountManagerViewController.h"
#import "User.h"
#import "AboutSystemViewController.h"

@interface mineViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)User        *user;

@end

@implementation mineViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //取得用户信息
        self.user = (User *)[NSKeyedUnarchiver unarchiveObjectWithFile:kUserPath];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.leftButton.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navtitle.text = @"我的";
    [self initWithView];
}

#pragma mark UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentifer = @"mineCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifer];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:indentifer];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (indexPath.row == 0) {
        
        // 背景图
        UIImageView *backGroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 138)];
        backGroundView.image = [UIImage imageNamed:@"BG_Home"];
        backGroundView.userInteractionEnabled = YES;
        [cell.contentView addSubview:backGroundView];
        
        // 头像
        UIImageView *photoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 30, 78, 78)];
        [photoImageView sd_setImageWithURL:[NSURL URLWithString:self.user.userHeadPhoto] placeholderImage:[UIImage imageNamed:@"btn_mine_User_normal"]];
        photoImageView.layer.masksToBounds = YES;
        photoImageView.layer.cornerRadius = 39;
        [cell.contentView addSubview:photoImageView];
        
        // 名字
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(photoImageView.frame) + 15, photoImageView.frame.origin.y + 13, 50, 30)];
        nameLabel.textColor = [UIColor HexStrWithColor:@"999999"];
        nameLabel.text = self.user.name;//@"张三";
        nameLabel.font = [UIFont systemFontOfSize:20 weight:2];
        [cell.contentView addSubview:nameLabel];
        
        // 物流公司
        UILabel *logisticsLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(nameLabel.frame) , photoImageView.frame.origin.y + 13, 100, 30)];
        logisticsLabel.font = [UIFont systemFontOfSize:15 weight:2];
        logisticsLabel.textColor = [UIColor HexStrWithColor:@"999999"];
        logisticsLabel.text = self.user.orgName;//@"东长物流";
        [cell.contentView addSubview:logisticsLabel];
        
        // 电话图标
        UIImageView *phoneNumberImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(photoImageView.frame) + 7, CGRectGetMaxY(nameLabel.frame), 34, 34)];
        phoneNumberImageView.image = [UIImage imageNamed:@"icon_Mine_phone"];
        [cell.contentView addSubview:phoneNumberImageView];
        
        // 电话号码
        UILabel *phoneNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(phoneNumberImageView.frame), CGRectGetMaxY(nameLabel.frame), 200, 30)];
        phoneNumberLabel.font = [UIFont systemFontOfSize:15 weight:2];
        phoneNumberLabel.text = self.user.telNum;//@"13714666551";
        phoneNumberLabel.textColor = [UIColor HexStrWithColor:@"999999"];
        [cell.contentView addSubview:phoneNumberLabel];
        
        UILabel *bottomLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(backGroundView.frame), kUIScreenWidth, 10)];
        bottomLabel.backgroundColor = [UIColor HexStrWithColor:@"eeeeee"];
        [cell.contentView addSubview:bottomLabel];
        
        return cell;
    }else if (indexPath.row == 1){
         cell.backgroundColor = [UIColor whiteColor];
        // 上边线
        UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 0.5)];
        topLabel.backgroundColor = [UIColor HexStrWithColor:@"cccccc"];
        [cell.contentView addSubview:topLabel];
        
        // 系统关于图标
        UIImageView *systemImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 6, 32, 32)];
        systemImageView.image = [UIImage imageNamed:@"icon_Mine_system"];
        [cell.contentView addSubview:systemImageView];
        
        // 系统关于
        UILabel *systemLabel = [[UILabel alloc] init];
        systemLabel.frame = CGRectMake(CGRectGetMaxX(systemImageView.frame), 0, kUIScreenWidth, 44);
        systemLabel.text = @"系统关于";
        [cell.contentView addSubview:systemLabel];
        
        // 下边线
        UILabel *bottomLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 0.5)];
        bottomLabel.backgroundColor = [UIColor HexStrWithColor:@"cccccc"];
        [cell.contentView addSubview:bottomLabel];
        
        return cell;
    } else{
        return cell;
    }
    
    /*
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.row == 0) {
        cell.textLabel.text = @"东长集团";
        cell.detailTextLabel.text = @"\n手机号:1372834463722";
        cell.detailTextLabel.numberOfLines = 0;
        cell.imageView.image = [UIImage imageNamed:@"tx_mr"];
    }  else if (indexPath.row == 1){
        cell.textLabel.text = @"系统关于";
    }
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
     */
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        AccountManagerViewController *accountManagerVC = [[AccountManagerViewController alloc] initWithUser:self.user];
        [self.navigationController pushViewController:accountManagerVC animated:YES];
    }  else if (indexPath.row == 1){
        NSLog(@"系统关于");
        AboutSystemViewController *aboutSystemVC = [[AboutSystemViewController alloc] init];
        [self.navigationController pushViewController:aboutSystemVC animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 148;
    } else if (indexPath.row == 1 || indexPath.row == 2){
        return 44;
    }
    return 44;
}

- (void)initWithView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor HexStrWithColor:@"eeeeee"];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
